//
//  yAxisView.m
//  3333
//
//  Created by iMac on 16/11/10.
//  Copyright © 2016年 zws. All rights reserved.
//

#import "YAxisView.h"
#import "DevChartModel.h"
#import "ChartYAxisModel.h"

#define topMargin 25   // 为顶部留出的空白
#define xAxisTextGap 5 //x轴文字与坐标轴间隙
#define numberOfYAxisElements 5 // y轴分为几段
#define kChartLineColor         [UIColor grayColor]
#define kChartTextColor         [UIColor blackColor]

@interface YAxisView()

@property (nonatomic ,strong) UILabel *unitLab;

@property (nonatomic ,strong) UIView *lineV;

@property (nonatomic ,strong) NSMutableArray *labsArray;

@end


@implementation YAxisView


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height-22)];
        [view setBackgroundColor:UIColor.whiteColor];
        [self addSubview:view];
        
        // 计算坐标轴的位置以及大小
        NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:10]};
        CGSize labelSize = [@"22-22\r\n22:22" sizeWithAttributes:attr];
        labelSize.height = 25;
        // 垂直坐标轴
        _lineV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, self.frame.size.height - labelSize.height - xAxisTextGap+1)];
        _lineV.backgroundColor = kChartLineColor;
        [_lineV setHidden:YES];
        [self addSubview:_lineV];
        
        _unitLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0,self.frame.size.width ,12)];
        [_unitLab setTextAlignment:NSTextAlignmentCenter];
        [_unitLab setFont:[UIFont systemFontOfSize:10]];
        [_unitLab setTextColor:UIColor.blackColor];
        [self addSubview:_unitLab];
        
        // Label做占据的高度
        CGFloat allLabelHeight = self.frame.size.height - xAxisTextGap - labelSize.height;
        // Label之间的间隙
        CGFloat labelMargin = (allLabelHeight - topMargin) / numberOfYAxisElements;
        
        // 添加Label
        for (int i = 0; i < numberOfYAxisElements + 1; i++) {
            UILabel *label = [[UILabel alloc] init];
            [label setBackgroundColor:UIColor.whiteColor];
            label.textAlignment = NSTextAlignmentLeft;// UITextAlignmentRight;
            label.font = [UIFont systemFontOfSize:10];
            label.adjustsFontSizeToFitWidth = YES;
            label.minimumScaleFactor = 0.5;
            [label.layer setBorderWidth:0];
            label.textColor = kChartTextColor;
            label.frame = CGRectMake(5, allLabelHeight - labelMargin* i - labelSize.height/2, self.frame.size.width - 1 - 5, labelSize.height);
            [self addSubview:label];
            [self.labsArray addObject:label];
        }
    }
    return self;
}

- (NSMutableArray *)labsArray{
    if (!_labsArray) {
        _labsArray = [NSMutableArray array];
    }
    return _labsArray;
}


- (id)initWithFrame:(CGRect)frame yMax:(CGFloat)yMax yMin:(CGFloat)yMin type:(NSInteger)type{
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height-22)];
        [view setBackgroundColor:UIColor.whiteColor];
        
        [self addSubview:view];
        
        // 计算坐标轴的位置以及大小
        NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:10]};
        CGSize labelSize = [@"22-22\r\n22:22" sizeWithAttributes:attr];
        // 垂直坐标轴
        UIView *separate = [[UIView alloc] initWithFrame:CGRectMake(type == 0?(self.frame.size.width-1):0, 0, 1, self.frame.size.height - labelSize.height - xAxisTextGap)];
        separate.backgroundColor = kChartLineColor;
        [self addSubview:separate];

    
        // Label做占据的高度
        CGFloat allLabelHeight = self.frame.size.height - xAxisTextGap - labelSize.height;
        // Label之间的间隙
        CGFloat labelMargin = (allLabelHeight - topMargin) / numberOfYAxisElements;
        
        // 添加Label
        for (int i = 0; i < numberOfYAxisElements + 1; i++) {
            UILabel *label = [[UILabel alloc] init];
            [label setBackgroundColor:UIColor.whiteColor];
            double avgValue = (yMax - yMin) / numberOfYAxisElements;
            double value = yMin + avgValue * i;
            
            if (value >= 1000000) {
                label.text = [NSString stringWithFormat:@"%.0f万", value/10000.0];
            }
            else {
                // 判断是不是小数
                if ([self isPureFloat:value]) {
                    label.text = [NSString stringWithFormat:@"%.2f", value];
                }
                else {
                    label.text = [NSString stringWithFormat:@"%.0f", value];
                }
            }
            
            label.textAlignment = type == 0 ? NSTextAlignmentRight:NSTextAlignmentLeft;// UITextAlignmentRight;
            label.font = [UIFont systemFontOfSize:10];
            label.textColor = kChartTextColor;
            label.frame = CGRectMake(5, allLabelHeight - labelMargin* i - labelSize.height/2, self.frame.size.width-1-5, labelSize.height);
            [self addSubview:label];
        }

        
        
    }
    return self;
}

// 判断是小数还是整数
- (BOOL)isPureFloat:(double)num
{
    int i = num;
    
    double result = num - i;
    
    // 当不等于0时，是小数
    return result != 0;
}

- (void)setType:(NSInteger)type{
    _type = type;
    for (UILabel *lab in self.labsArray) {
        lab.textAlignment = _type == 0 ? NSTextAlignmentRight:NSTextAlignmentLeft;
        CGRect frame = lab.frame;
        frame.origin.x = _type == 0 ? 0:5;
        lab.frame = frame;
    }
    
    CGRect rect = _lineV.frame;
    rect.origin.x = _type == 0?(self.frame.size.width-1):0;
    [_lineV setFrame:rect];
    
}

- (void)setChartModel:(DevChartModel *)chartModel{
    _chartModel = chartModel;

    [_unitLab setText:@""];
    
    for (UILabel *lab in self.labsArray) {
        lab.text = @"--";
    }
    
    if ( _chartModel.yAxisArray.count == 0||!(self.type<_chartModel.yAxisArray.count)) {
        return;
    }
    
    __block CGFloat yMax = 0.0;
    __block CGFloat yMin = 0.0;
    kWeakSelf;
    [_chartModel.yAxisArray enumerateObjectsUsingBlock:^(ChartYAxisModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //NSLog(@"obj.y_max === %f ,obj.y_min === %f",obj.y_max,obj.y_min);
        if (weakSelf.type == idx) {
             yMax = obj.y_max;
             yMin = obj.y_min;
             [weakSelf.unitLab setText:ValidStr(obj.y_unit)?obj.y_unit:@""];
        }
    }];
    
    NSInteger i = 0;
    for (UILabel *lab in self.labsArray) {
        double avgValue = (yMax - yMin) / numberOfYAxisElements;
        double value = yMin + avgValue * i;
        if (value >= 1000000) {
            lab.text = [NSString stringWithFormat:@"%.1fw", (value/10000.0)];
        }else {
             //判断是不是小数
            if ([self isPureFloat:value]) {
                lab.text = [NSString stringWithFormat:@"%.1f",value];
            }
            else {
                lab.text = [NSString stringWithFormat:@"%.1f",value];
            }
        }
        i++;
    }
}

@end
